<?php

class Front_ExameController extends ExtLb_Controller_Action
{
	private $questionExameTbl;
	private $questionTbl;
	private $exame;
	private $markTbl;
	
	private $auth;
	private $storage;
	
	private $exameSessSet;
	private $exameSessGet;
	
    public function init()
    {
        $this->loadCss('default', 'exame');
        
        $this->questionExameTbl	= new Admin_Model_Kiemtracauhoi();
        $this->exame			= new Admin_Model_Kiemtra();
        $this->questionTbl		= new Admin_Model_Question();
        $this->markTbl			= new Admin_Model_Mark();
        
        $this->auth				= Zend_Auth::getInstance();
        $this->storage 			= $this->auth->getIdentity();
        
        $this->exameSessSet		= new Zend_Session_Namespace('exameSess');
    }

    public function indexAction()
    {
    	if(!$this->auth->hasIdentity()){
    		$this->jShowMessage("Hãy đăng nhập để có thể sử dụng chức năng này");
    		$this->_forward('index','index','front');
    	}else {
    		$exame	= $this->exame->fetchAllKiemTra();
    		
    		$this->view->listExames	=	$exame;
    		
    	}
    }

    public function doexameAction(){
	    if(isset($_GET['id']) && isset($_GET['do']) && !empty($_GET['id'])){
	    	
	    	$selectQuestion = $this->questionExameTbl->listTestQuestion('kt.idKiemTra ='.$_GET['id'],'rand()');
	    	
	    	$exameCurrentDo = $this->exame->fetchKiemTra($_GET['id']);
	    	
	    	$paginatorExame	= new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($selectQuestion));
	    	$paginatorExame->setItemCountPerPage(); // Hien thi tat ca cac cau hoi
//	    					->setPageRange(3)
//	    					->setCurrentPageNumber($this->_getParam('page',1));
	    					
	    	//SESSION
	    	// === Tao session de luu tru ma bai kiem tra dang lam
	    	$this->exameSessSet->idKiemTra = $_GET['id'];
	    	$this->refreshSession();
	    	
	    	//
	    	// === Kiem tra xem bai test da duoc lam lan nao hay chua
			if(!$this->isMadeExame(
						$this->storage->username, 
						$this->exameSessGet['idKiemTra'],
						date('d/m/y'))
				){

					//SESSION
	    		$this->exameSessSet->expireTime	= time()+ $exameCurrentDo['thoiGian']*60;
	    		$this->exameSessSet->username	= $this->auth->getIdentity()->username;
	    		$this->refreshSession();
	    		
	    		$timeExpire = $this->exameSessGet['expireTime'] - time();
	    		$this->view->second =  	$timeExpire;
	    		
	    		// === Ghi thong tin username lam bai vao file de doi chiu lan sau.
	    		$this->setDoExame(
	    					$this->exameSessGet['idKiemTra'],
	    					$this->auth->getIdentity()->username,
	    					date('d/m/y')
	    			);
	    	}else{
	    		$this->jDialogMessage("Thông báo", "Bài này bạn chỉ được phép làm 1 lần");
	    		$this->_forward('index','exame','front');
	    	}
	    	$this->refreshSession();
	    	
	    	$this->view->exameCurrentDo			= $exameCurrentDo;
	    	$this->view->paginatorExame			= $paginatorExame;
	    	
    		
    	}
    }
    
	public function getexameAction(){
    	if(isset($_POST['cmdDoneExame'])){
    		$totalNums = Zend_Session::namespaceGet('numRowQuesstion');
    		$date = getdate();
    		
    		$numQuessTrue = 0; //so cau hoi tra loi dung
    		
    		//SESSION
    		$this->refreshSession();
    		
    		// === Dem so cau tra loi dung
    		for ($i=1; $i <= $totalNums['total']; $i++){
				$quess = !empty($_POST[$i]) ? $_POST[$i] : null;
				$youChoic = explode("|", $quess);
				
				if($quess!=null && $this->isQuestionTrue((int)$youChoic[1], $youChoic[0])){
					$numQuessTrue++;
				}
    		}

    		// === Tinh diem cho bai trac nghiem
    		$mark =  round(($numQuessTrue*10)/$totalNums['total'],0);
    		
    		//
    		// === Kiem tra "username" và môn học da có hay chưa
    		// === If da co thì bổ xung
    		// === Chua co thi them moi
    		if($this->markTbl->issetUsernameAndMonHoc(
    							$this->storage->username,
    							$this->exame->getIdMonHoc($this->exameSessGet['idKiemTra']),
    							$this->_getParam('hd-hocKy'),
    							$this->_getParam('hd-namHoc')
    					)){

    			//
    			$this->markTbl->updateHeSo(
    					$this->exameSessGet['username'],
    					$this->exame->getIdMonHoc($this->exameSessGet['idKiemTra']),
    					$mark,
    					$this->exame->getHeSoKiemTra($this->exameSessGet['idKiemTra'])
    					
    			);
    		}else{
    		
    			$this->markTbl->addMark(
    					$this->exameSessGet['username'], 
    					$this->exame->getIdMonHoc(
		    						$this->exameSessGet['idKiemTra']
		    					),
    					$mark,
    					$this->exame->getHeSoKiemTra($this->exameSessGet['idKiemTra']),
    					$this->_getParam('hd-hocKy'),
    					$date['year']
    				);
    		}
    		
    		$this->_redirect('/front/mark/');
    	}
    }
    
    public function viewexameAction(){
    	
    }
    
    private function refreshSession($namespace="exameSess"){
    	$this->exameSessGet = Zend_Session::namespaceGet($namespace);
    }
    
    private function isQuestionTrue($idQues,$answerChoic){
    	$ques = $this->questionTbl->fetchQuestion($idQues);

    	if($answerChoic == $ques['dapAnDung']) return TRUE;
    	else {
    		return FALSE;
    	}
    }

}









